package 线程不安全;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-10-05
 * Time: 21:21
 */
class Counter{
    //静态的count
    static int count = 0;
     public void increase(){
         synchronized (Counter.class){
             synchronized (this){
                 count++;
             }

         }
    }
}
public class demo1 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Counter counter1 = new Counter();
        //线程1 count自增10000次
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    counter.increase();
                }
            }
        };
        //线程2 count也自增10000次
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    counter1.increase();
                }
            }
        };
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(Counter.count);
    }

}
